home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / TCL1 / CNUMBERE / CNUMBERE.C next >
Text File  |  1991-07-04  |  5KB  |  241 lines

  1. /*            Methods for CNumberEditor                */
  2.  
  3. #include "CNumberEditor.h"
  4. #include "SANE.h"
  5.  
  6. extern    CBureaucrat        *gGopher;
  7. extern    CursHandle        gIBeamCursor;
  8. extern    short            gClicks;
  9. static    CNumberEditor    *gNumberEditor;
  10.  
  11. void    CNumberEditor::INumberEditor(CView *anEnclosure, CBureaucrat *aSupervisor,
  12.                                          Str255 aFontName, int aFontSize,
  13.                                         Rect *aRect, short sigDigs,long setComm)
  14. {
  15.     Rect        stringRect;
  16.     short        aWidth, aHeight;
  17.     
  18.     aWidth=aRect->right-aRect->left;
  19.     aHeight=aRect->bottom-aRect->top;
  20.     
  21.     inherited::IPane(anEnclosure, aSupervisor,aWidth, aHeight,aRect->left, aRect->top,
  22.                             sizFIXEDSTICKY, sizFIXEDSTICKY);
  23.     
  24.     wantsClicks=TRUE;
  25.     dirty=FALSE;
  26.     significantDigits=sigDigs;
  27.     GetFNum(aFontName, &(itsStyle.tsFont));
  28.     if(aFontSize==0)
  29.     {
  30.         if(sigDigs<0)
  31.             aFontSize=.7*aHeight;
  32.         else
  33.             aFontSize=aHeight-3;
  34.     }
  35.     else
  36.     {
  37.         if(sigDigs<0)
  38.             aFontSize=(aFontSize>(0.7*aHeight))?(0.7*aHeight):aFontSize;
  39.         else
  40.             aFontSize=(aFontSize>(aHeight-3))?(aHeight-3):aFontSize;
  41.     }
  42.         
  43.     itsStyle.tsSize=aFontSize;
  44.     
  45.     SetCommandNumber=setComm;
  46.     stringRect=frame;
  47.     stringRect.left+=1;stringRect.right-=1;stringRect.top+=1;stringRect.bottom-=2;
  48.     this->Prepare();
  49.     itsTEHandle=TEStylNew(&stringRect, &stringRect);
  50.     TESetStyle(doFont+doSize, &itsStyle, TRUE, itsTEHandle);
  51.     TEAutoView(FALSE, itsTEHandle);
  52.     (*itsTEHandle)->just=teJustLeft;
  53.     TECalText(itsTEHandle);
  54. }
  55.  
  56. void    CNumberEditor::Dispose(void)
  57. {
  58.     TEDispose(itsTEHandle);
  59.     inherited::Dispose();
  60. }
  61.  
  62. void    CNumberEditor::SetString(Str255    aString)
  63. {
  64.     Ptr            aPointer;
  65.     long        numCharacters;
  66.     
  67.     aPointer=(Ptr)aString;
  68.     aPointer+=1;
  69.     numCharacters=(long)aString[0];
  70.     TESetText(aPointer, numCharacters, itsTEHandle);
  71.     this->SelectAll();
  72.     this->Refresh();
  73. }
  74.  
  75. void    CNumberEditor::GetString(Str255    aString)
  76. {
  77.     register int    i;
  78.     Handle            text;
  79.     long            size;
  80.     
  81.     text=(*itsTEHandle)->hText;
  82.     size=GetHandleSize(text);    
  83.     for(i=0;i<size;i++)
  84.         aString[i]=((char*)(*text))[i];
  85.     aString[size]='\0';
  86.     CtoPstr(aString);
  87. }
  88.  
  89. void    CNumberEditor::SelectAll(void)
  90. {
  91.     int        aLength;
  92.     
  93.     aLength=(*itsTEHandle)->teLength;
  94.     this->Prepare();
  95.     TESetSelect(0, aLength, itsTEHandle);
  96.     TESetStyle(doFont+doSize, &itsStyle, FALSE, itsTEHandle);
  97.     TECalText(itsTEHandle);
  98. }
  99.  
  100. double    CNumberEditor::GetDoubleValue(void)
  101. {
  102.     
  103.     Str255            aString;
  104.     double            value, *doublePtr;
  105.     extended        num80;
  106.     DecForm            aFormat;
  107.     
  108.     this->GetString(aString);
  109.     
  110.     num80=str2num(aString);
  111.     
  112.     if(sizeof(double)==12)
  113.         x80tox96(&num80,&value);
  114.     else
  115.     {
  116.         doublePtr=(double*)(&num80);
  117.         value=*doublePtr;
  118.     }
  119.     
  120.     aFormat.style = FLOATDECIMAL;
  121.     aFormat.digits = significantDigits;
  122.     num2str(&aFormat, num80, aString);
  123.     
  124.     this->SetString(aString);
  125.     dirty=FALSE;
  126.     return    value;
  127. }
  128.  
  129. void    CNumberEditor::SetDoubleValue(double    newValue)
  130. {
  131.     Str255            aString;
  132.     extended        num80, *num80Ptr;
  133.     DecForm            aFormat;
  134.     
  135.     aFormat.style = FLOATDECIMAL;
  136.     aFormat.digits = significantDigits;
  137.     
  138.     if(sizeof(double)==12)
  139.     {
  140.         x96tox80(&newValue,&num80);
  141.         num2str(&aFormat, num80, aString);
  142.     }
  143.     else
  144.     {
  145.         num80Ptr=(extended*)(&newValue);
  146.         num2str(&aFormat, *num80Ptr, aString);
  147.     }
  148.  
  149.     this->SetString(aString);
  150.     dirty=FALSE;
  151. }
  152.  
  153. long    CNumberEditor::GetLongValue(void)
  154. {
  155.     long            value;
  156.     Str255            aString;        
  157.     
  158.     this->GetString(aString);
  159.     StringToNum(aString, &value);
  160.     NumToString(value,aString);
  161.     this->SetString(aString);
  162.     dirty=FALSE;
  163.     return    value;
  164. }
  165.  
  166. void    CNumberEditor::SetLongValue(long    newValue)
  167. {
  168.     Str255            aString;        
  169.     
  170.     dirty=FALSE;
  171.     NumToString(newValue,aString);
  172.     this->SetString(aString);
  173. }
  174.  
  175. void    CNumberEditor::Draw(Rect    *area)
  176. {
  177.     FrameRect(&frame);
  178.     TEUpdate(area, itsTEHandle);
  179. }
  180.  
  181. void    CNumberEditor::DoKeyDown(char theChar, Byte keyCode, EventRecord *macEvent)
  182. {
  183.     if((theChar==13)||(theChar==3))
  184.     {
  185.         itsSupervisor->DoCommand(SetCommandNumber);
  186.         this->SelectAll();
  187.         dirty=FALSE;
  188.     }
  189.     else
  190.     {
  191.         dirty=TRUE;
  192.         this->Prepare();
  193.         TEKey(theChar, itsTEHandle);
  194.     }
  195. }
  196.  
  197. void    CNumberEditor::DoClick(Point hitPt, short modifierKeys, long when)
  198. {
  199.     if(gClicks==2)
  200.         this->SelectAll();
  201.     else
  202.         TEClick(hitPt, FALSE, itsTEHandle);
  203. }
  204.  
  205.  
  206. void    CNumberEditor::AdjustCursor(Point where, RgnHandle    mouseRgn)
  207. {
  208.     SetCursor(*gIBeamCursor);
  209.     gGopher=this;
  210.     if(gNumberEditor!=this)
  211.     {
  212.         this->Activate();
  213.         this->SelectAll();
  214.     }
  215. }
  216.  
  217. void    CNumberEditor::Dawdle(long    *sleep)
  218. {
  219.     Prepare();
  220.     TEIdle(itsTEHandle);
  221.     *sleep = GetCaretTime();
  222. }
  223.  
  224. void    CNumberEditor::Activate(void)
  225. {
  226.     inherited::Activate();
  227.     if(gNumberEditor)
  228.         gNumberEditor->Deactivate();
  229.     gNumberEditor=this;
  230.     gGopher=this;
  231.     this->Prepare();
  232.     TEActivate(itsTEHandle);
  233. }
  234.  
  235. void    CNumberEditor::Deactivate(void)
  236. {
  237.     inherited::Deactivate();
  238.     this->Prepare();
  239.     TEDeactivate(itsTEHandle);
  240.     gNumberEditor=0L;
  241. }